using System;
using System.Collections.Specialized;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using MetaBuilders.Irc.Messages;

namespace MetaBuilders.NeboTests
{

	[TestClass]
	public class IrcxTests : MessageTester
	{

		[TestMethod]
		public void Whisper()
		{
			String raw = "WHISPER #example user1,user2 :Hello World";
			WhisperMessage msg = AssertRoundTripAndType( raw, typeof( WhisperMessage ) ) as WhisperMessage;
			Assert.AreEqual( "#example", msg.Channel, "Channel" );
			Assert.AreEqual( "Hello World", msg.Text, "Text" );
			Assert.AreEqual( 2, msg.Targets.Count, "Targets Count" );
			Assert.AreEqual( "user1", msg.Targets[ 0 ], "Targets" );
			Assert.AreEqual( "user2", msg.Targets[ 1 ], "Targets" );
		}

		[TestMethod]
		public void ChannelProperty()
		{
			String[] raws = new String[] {
				"PROP #testing ONJOIN :Welcome to the stupidest channel ever."
				,"PROP #testing ONJOIN"
				,"PROP #testing *"
			};
			String[] channels = new String[] {
				"#testing"
				,"#testing"
				,"#testing"
			};
			String[] properties = new String[] {
				"ONJOIN"
				,"ONJOIN"
				,""
			};
			String[] values = new String[] {
				"Welcome to the stupidest channel ever."
				,""
				,""
			};
			for ( int i = 0; i < raws.Length; i++ )
			{
				ChannelPropertyMessage msg = AssertRoundTripAndType( raws[ i ], typeof( ChannelPropertyMessage ) ) as ChannelPropertyMessage;
				Assert.AreEqual( channels[ i ], msg.Channel, "Channel" );
				Assert.AreEqual( properties[ i ], msg.Prop, "Property" );
				Assert.AreEqual( values[ i ], msg.NewValue, "New Value" );
			}
		}

		[TestMethod]
		public void ChannelPropertyReply()
		{
			String[] raws = new String[] {
				":chat3 818 olene #testing ONJOIN :Welcome to the stupidest channel ever."
				,":chat3 818 olene #testing GUID {}"
				,":chat3 818 olene #testing CREATOR olene[oqd@proxy38.safetypin.net]"
			};
			String[] channels = new String[] {
				"#testing"
				,"#testing"
				,"#testing"
			};
			String[] properties = new String[] {
				"ONJOIN"
				,"GUID"
				,"CREATOR"
			};
			String[] values = new String[] {
				"Welcome to the stupidest channel ever."
				,"{}"
				,"olene[oqd@proxy38.safetypin.net]"
			};
			for ( int i = 0; i < raws.Length; i++ )
			{
				ChannelPropertyReplyMessage msg = AssertRoundTripAndType( raws[ i ], typeof( ChannelPropertyReplyMessage ) ) as ChannelPropertyReplyMessage;
				Assert.AreEqual( channels[ i ], msg.Channel, "Channel" );
				Assert.AreEqual( properties[ i ], msg.Prop, "Property" );
				Assert.AreEqual( values[ i ], msg.Value, "New Value" );
			}
		}

		[TestMethod]
		public void ChannelPropertyEndReply()
		{
			String raw = ":chat3 819 olene #testing :End of properties";
			ChannelPropertyEndReplyMessage msg = AssertRoundTripAndType( raw, typeof( ChannelPropertyEndReplyMessage ) ) as ChannelPropertyEndReplyMessage;
			Assert.AreEqual( "#testing", msg.Channel, "Channel" );
		}

		[TestMethod]
		public void IsIrcx()
		{
			AssertRoundTripAndType( "ISIRCX", typeof( IsIrcxMessage ) );
			AssertRoundTripAndType( "IRCX", typeof( IrcxMessage ) );
		}

		[TestMethod]
		public void IrcxReply()
		{
			String raw;
			IrcxReplyMessage msg;

			raw = ":server 800 you 0 1 ANON 512 *";
			msg = AssertRoundTripAndType( raw, typeof( IrcxReplyMessage ) ) as IrcxReplyMessage;
			Assert.IsFalse( msg.IsIrcxClientMode, "IsIrcxClientMode" );
			Assert.AreEqual( "1", msg.Version, "Version" );
			Assert.AreEqual( 1, msg.AuthenticationPackages.Count, "Authentication Packages Count" );
			Assert.AreEqual( "ANON", msg.AuthenticationPackages[ 0 ], "Authentication Packages" );
			Assert.AreEqual( 512, msg.MaximumMessageLength, "Message Length" );
			Assert.AreEqual( "*", msg.Tokens, "Tokens" );

			raw = ":server 800 you 1 1 ANON 512 *";
			msg = AssertRoundTripAndType( raw, typeof( IrcxReplyMessage ) ) as IrcxReplyMessage;
			Assert.IsTrue( msg.IsIrcxClientMode, "IsIrcxClientMode" );
			Assert.AreEqual( "1", msg.Version, "Version" );
			Assert.AreEqual( 1, msg.AuthenticationPackages.Count, "Authentication Packages Count" );
			Assert.AreEqual( "ANON", msg.AuthenticationPackages[ 0 ], "Authentication Packages" );
			Assert.AreEqual( 512, msg.MaximumMessageLength, "Message Length" );
			Assert.AreEqual( "*", msg.Tokens, "Tokens" );

		}

		[TestMethod]
		public void Knock()
		{
			String raw = "KNOCK #testing";
			KnockMessage msg = AssertRoundTripAndType( raw, typeof( KnockMessage ) ) as KnockMessage;
			Assert.AreEqual( "#testing", msg.Channel, "Channel" );
		}

		[TestMethod]
		public void KnockRequest()
		{
			String raw = ":irc.foxlink.net 710 #artificer2 #artificer2 artificer!artificer@12-255-177-172.client.attbi.com :has asked for an invite.";
			KnockRequestMessage msg = AssertRoundTripAndType( raw, typeof( KnockRequestMessage ) ) as KnockRequestMessage;
			Assert.AreEqual( "#artificer2", msg.Channel, "Channel" );
			Assert.AreEqual( "artificer!artificer@12-255-177-172.client.attbi.com", msg.Knocker.ToString(), "Knocker" );
		}

		[TestMethod]
		public void KnockReply()
		{
			String raw = ":irc.foxlink.net 711 artificer #artificer :Your KNOCK has been delivered.";
			KnockReplyMessage msg = AssertRoundTripAndType( raw, typeof( KnockReplyMessage ) ) as KnockReplyMessage;
			Assert.AreEqual( "#artificer", msg.Channel, "Channel" );
		}

	}

}

